package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.business.Supplier;
import com.marktotop.mapper.SupplierMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.vo.business.VaccineSupplierVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {

    @Autowired
    private SupplierMapper supplierMapper;


    /**
     * 供应商列表展示，分页，模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param supplier
     * @return
     */
    @Override
    public ResultBean findSupplierList(Integer pageNum, Integer pageSize, Supplier supplier) {
        // 开启分页
        Page<Supplier> page = new Page<>(pageNum,pageSize);
        IPage<Supplier> SupplierByPage = supplierMapper.getSupplierByPage(page,supplier);
        if (SupplierByPage.getRecords().isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有供应商信息成功！")
                .data("total",SupplierByPage.getTotal())
                .data("SupplierList",SupplierByPage.getRecords());
    }

    /**
     * 添加新的供应商
     *
     * @param supplier
     * @return
     */
    @Override
    public boolean addSupplier(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        return supplierMapper.insert(supplier) == 1;
    }


    /**
     * 获取供应商信息，带联系人信息
     *
     * @param id
     * @return
     */
    @Override
    public Supplier getSupplier(Long id){
        Supplier supplier = supplierMapper.selectSupplierById(id);
        return supplier;
    }

    /**
     * 获取所有疫苗供应商列表，供疫苗登记使用
     *
     * @return
     */
    @Override
    public List<VaccineSupplierVO> findVaccineSupplierList() {
        return supplierMapper.getAllSupplierForVaccine();
    }


}
